﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Security.Cryptography;
using System.Text;
using Sebarf.Core.Internal;
using Sebarf.Security.UserPassword.Interfaces;
using Sebarf.Services.Interfaces;

#endregion

namespace Sebarf.Security.Authentication {
	/// <summary>
	/// The username/password authentication service. Used at client side to authenticate.
	/// </summary>
	public class UsernamePasswordAuthenticationRequestService : Service, IUsernamePasswordAuthenticationService {
		#region Public Properties

		[ServiceRequest]
		public IRequestRemoteHandlerEngineLocatorService RequestRemoteHandlerEngineLocatorService { get; set; }

		#endregion

		#region Public Methods

		// public methods located here

		#endregion

		#region Private Methods

		// private methods located here

		#endregion

		#region Private Fields

		private readonly MD5 md5algo = MD5.Create();

		#endregion

		#region IUsernamePasswordAuthenticationService Members

		/// <summary>
		/// Transmits an AuthenticationRequest to authenticate the given user by the specified password. 
		/// NOTE: THE PASSWORD WILL BE TRANSMITTED AS A SIMPLE MD5 HASH!
		/// </summary>
		/// <param name="id"></param>
		/// <param name="password"></param>
		/// <returns></returns>
		public bool Authenticate(string id, string password) {
			return
				(bool)
				RequestRemoteHandlerEngineLocatorService.GetRemoteEngine().Process(new UsernamePasswordAuthenticationRequest {
					Username = id,
					PasswordMD5Hash =
						Convert.ToBase64String(
						md5algo.ComputeHash(
							Encoding.UTF8.GetBytes(password)))
				});
		}

		#endregion
	}
}